package cave.renderer;


public class Animation {
	
	public enum Mode {
		LOOP,
		ONE_SHOT,
		REVERSED_LOOP,
		REVERSED_ONE_SHOT
	}
	
	private Texture[] frames;
	private float fps;
	
	public Animation () {

	}
	
	void setFps(float fps) {
		this.fps = fps;
	}
	
	void setFrames(Texture[] frames) {
		this.frames = frames;
	}
	
	public int getNumFrames() {
		return frames.length;
	}
	
	public Texture[] getFrames() {
		return frames;
	}

	public Texture getFrame(float time, Mode mode) {
		switch (mode) {
			case LOOP:
			{
				int animationProgress = (int)(time/(1.0f/fps)) % frames.length;
				return frames[animationProgress];
			}
			
			case ONE_SHOT:
			{
				int animationProgress = (int)(time/(1.0f/fps));
				if (animationProgress >= frames.length){
					return frames[frames.length-1];
				}
				return frames[animationProgress];
			}
			
			case REVERSED_LOOP:
			{
				int animationProgress = (int)(time/(1.0f/fps)) % frames.length;
				return frames[frames.length-1-animationProgress];
			}
				
			case REVERSED_ONE_SHOT:
			{
				int animationProgress = (int)(time/(1.0f/fps));
				if (animationProgress >= frames.length){
					return frames[0];
				}
				return frames[frames.length-1-animationProgress];
			}
		}
		return null;
	}
	
	public void incUsers() {
		frames[0].getAtlas().incUsers();
	}
	
	public void decUsers() {
		frames[0].getAtlas().decUsers();
	}
}
